//
//  NSDictionary+YDChangeNull.m
//  YiDian
//
//  Created by iOS on 2018/8/7.
//  Copyright © 2018年 Jeagine. All rights reserved.
//

#import "NSDictionary+YDChangeNull.h"

@implementation NSDictionary (YDChangeNull)

// 改变对象的值，不能为空
- (nonnull id)yd_objectForKeyWithoutNull:(nonnull id)aKey
{
    id result = [self objectForKey:aKey];
    if (!result || [result isKindOfClass:[NSNull class]]) {
        result = @"";
        return result;
    }
    if ([result isKindOfClass:[NSNumber class]]) {
        return [result stringValue];
    }
    if ([result isKindOfClass:[NSDictionary class]] || [result isKindOfClass:[NSArray class]]) {
        return result;
    }
    
    return result;
}

// 改变对象的值，可为空或为0
- (nullable id)yd_objectForKeyCanNull:(nonnull id)aKey
{
    id result = [self objectForKey:aKey];
    if (!result || [result isKindOfClass:[NSNull class]]) {
        return nil;
    }
    if ([result isKindOfClass:[NSDictionary class]] || [result isKindOfClass:[NSArray class]]) {
        if ([result count] == 0) {
            return nil;
        }
        return result;
    }
    if ([result isKindOfClass:[NSNumber class]]) {
        if ([result isEqual: @0]) {
            return nil;
        }
    }
    return result;
}

@end
